//
//  PopupWindow.m
//  VideoExam
//
//  Created by apple on 2020/4/17.
//

#import "PopupWindow.h"
@interface PopupWindow ()
@property(nonatomic,strong)UIView * baseView;
@property(nonatomic,assign)int currentType;
@property(nonatomic,strong)NSString * title;
@property(nonatomic,strong)NSString * content;
@property(nonatomic,strong)UILabel * contentLabel;
@property(nonatomic,strong)NSArray * selectedTitles;
@property(nonatomic,assign)PopupWindowShowType showType;
@property(nonatomic,strong)UIButton * cancel_btn, * determine_btn;
@property(nonatomic,strong)UIView * bottomView;
@end
@implementation PopupWindow

- (instancetype)initWithFrame:(CGRect)frame showType:(PopupWindowShowType)showType  {
    self = [super initWithFrame:frame];
    if (self) {
        self.showType=showType;
        self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
        [self uiSet];
    }
    return self;
}

/// 自定义内容的初始化方式
/// @param frame frame
/// @param content content
/// @param showType showType
- (instancetype)initWithFrame:(CGRect)frame showContent:(NSString *)content showType:(PopupWindowShowType)showType  {
    self = [super initWithFrame:frame];
       if (self) {
           self.showType=showType;
           self.content=content;
           self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
           [self uiSet];
       }
       return self;
}
- (void)uiSet {
    self.currentType=0;//默认
    self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-104, 300)];
    self.baseView.backgroundColor=[UIColor whiteColor];
    self.baseView.layer.cornerRadius=12;
    self.baseView.layer.masksToBounds=YES;
    self.baseView.center=CGPointMake(self.width/2, self.height/2);
    [self addSubview:self.baseView];
    
    if (self.showType==Show_record_complete_type || self.showType==Show_record_complete_custom_content_type) {//视频录制横屏设置
        self.baseView.width=270;
        self.baseView.center=CGPointMake(self.width/2, self.height/2);
    }
    
    if (self.showType ==Show_agreement_type) { //服务协议及隐私政策
        self.baseView.width = SCREEN_WIDTH - 27- 27;
    }
    
    UILabel * titleLabel = [UIFactory create_LabelContent:self.title textColor:nil fontSize:17];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.font=[UIFont boldSystemFontOfSize:17];
    titleLabel.frame=CGRectMake(0, 16, self.baseView.width, 30);
    [self.baseView addSubview:titleLabel];
    
    
    self.contentLabel = [UIFactory create_LabelContent:self.content textColor:color(@"#FF888888") fontSize:14];
    self.contentLabel.numberOfLines=0;
    self.contentLabel.textAlignment=NSTextAlignmentCenter;
    self.contentLabel.frame=CGRectMake(15, titleLabel.bottom+10, self.baseView.width-30, 30);
    CGSize size= [self.contentLabel sizeThatFits:CGSizeMake(self.contentLabel.width, CGFLOAT_MAX)];
    self.contentLabel.width=size.width;
    self.contentLabel.height=size.height;
    self.contentLabel.left=(self.baseView.width/2)-(self.contentLabel.width/2);
    [self.baseView addSubview:self.contentLabel];
    
    
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentLabel.bottom+16, self.baseView.width, 50)];
    [self.baseView addSubview:self.bottomView];
 
    
    CGFloat btnWidth = self.bottomView.width /2;
    for (int i =0; i<self.selectedTitles.count; i++) {
        UIButton * btn = [UIFactory create_btnWithTitle:self.selectedTitles[i] imageName:nil backgroundColor:[UIColor whiteColor] TypefaceColor:nil fontSize:16 target:self action:@selector(btnClicked:)];
        btn.titleLabel.font  = [UIFont boldSystemFontOfSize:16];
        if (i==0) {
            [btn setTitleColor:RGBA(0, 0, 0, 0.45) forState:UIControlStateNormal];
            self.cancel_btn=btn;
        } else {
            [btn setTitleColor:theme_color forState:UIControlStateNormal];
            self.determine_btn=btn;
        }
        btn.tag=i+10;
        [self.baseView addSubview:btn];
        btn.frame=CGRectMake(i*btnWidth, 0.5, btnWidth, self.bottomView.height);
        [self.bottomView addSubview:btn];
    }

    UIView *line1 = [[UIView alloc] initWithFrame:CGRectZero];
    line1.backgroundColor = color(@"#F0F0F0");
    [self.bottomView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.right.top.mas_equalTo(0);
       make.height.mas_equalTo(0.5);
    }];

    UIView * line2 = [[UIView alloc] initWithFrame:CGRectZero];
    line2.backgroundColor = color(@"#F0F0F0");
    [self.bottomView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom);
        make.centerX.mas_equalTo(self.bottomView);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(self.bottomView.height);
    }];
    
    self.baseView.height = self.bottomView.bottom;
    self.baseView.center=self.center;
    [self set_style];
}
- (void)setShowType:(PopupWindowShowType)showType {
    _showType=showType;
    if (_showType==Show_update_type) {
        self.title=@"升级提醒";
//        self.content=@"“考评宝”已推出了新版，便于您的使用体验，请到App Store升级。”";
        self.selectedTitles=@[@"取消",@"升级",];
    } else if (_showType==Show_LogOut_type) {
         self.title=@"确认退出登录";
        self.content=@"您确定要退出登录吗？";
         self.selectedTitles=@[@"取消",@"退出",];
    } else if (_showType==Show_test_submit_type) {
         self.title=@"提示";
         self.selectedTitles=@[@"取消",@"确定提交",];
    } else if (_showType==Show_giveUp_test_type_0) {//放弃考试
        self.title=@"放弃考试";
        self.content=@"您确定要放弃考试吗?";
        self.selectedTitles=@[@"取消",@"放弃",];
    }else if (_showType==Show_giveUp_test_type_2) {//放弃考试
        self.title=@"放弃考试";
        self.content=@"部分失误并不会影响您的成绩，建议您坚持完成并上传。\n中途放弃考试，报名费不退，您确定要放弃考试么？";
        self.selectedTitles=@[@"取消",@"放弃",];
    } else if (_showType== Show_record_complete_type) {//录制完成 到时间自动停止
        self.title=@"录制完成";
        self.content=@"录制结束则消耗一次录制机会\n本次录制时长：10分00秒";
        self.selectedTitles=@[@"结束"];
    } else if (_showType == Show_record_complete_custom_content_type) {//录制完成 自定义内容
       self.title=@"录制完成";
       self.content=@"";
       self.selectedTitles=@[@"取消",@"确定"];
    } else if (_showType == Show_interrupt_test_type) {//中断考试
      self.title=@"中断考试";
      self.content=@"中断考试后，您可以从主页底部“考级历史”菜单内继续完成考试。\n考试期限：30日";
      self.selectedTitles=@[@"取消",@"中断考试"];
    }  else if (_showType == Show_not_finished_order_type) {//刚登陆是有未完成订单提示
         self.title=@"订单提示";
         self.content=@"检测到您有未完成的考试，是否继续考试？";
         self.selectedTitles=@[@"取消",@"继续考试"];
    }  else if (_showType == Show_upload_not_WIFI_type) {//视频上传时不是WiFi的情况下提示
        self.title=@"使用移动网络上传";
//        self.content=@"上传使用移动网络数据，是否继续上传？";
        self.selectedTitles=@[@"取消",@"上传"];
    } else if (_showType == Show_mandatory_update_type) {//强制更新
        self.title=@"提示更新";
//        self.content=@"有新版本需要更新";
        self.selectedTitles=@[@"升级"];
    } else if (_showType == Show_recovery_test_type) {//恢复考试
         self.title=@"恢复考试";
        self.content = @"您确定要恢复考试吗？";
        self.selectedTitles=@[@"取消",@"恢复"];
    } else if (_showType == Show_replace_type) {//该账号已在其他设备登录
         self.title=@"提醒";
        self.content = @"该账号已在其他设备登录\n请重新登录";
        self.selectedTitles=@[@"确认"];
    } else if (_showType == Show_agreement_type) {//服务协议及隐私政策
         self.title=@"服务协议及隐私政策";
        self.content = @"欢迎使用“云考级”，我们非常重视您的个人信息和隐私保护。在您使用“云考级”服务之前，请仔细阅读《用户政策》和《隐私协议》，我们将严格按照经您同意的各项条款使用您的个人信息，以便为您提供更好的服同，请您同意此政策和协议。\n请点击“同意”开始使用我们的产品和服务，我们将尽全力保护您的个人信息安全。";
        self.selectedTitles=@[@"不同意并退出",@"同意"];
    } else if (_showType == Show_address_type) {//邮寄地址提交提示
         self.title=@"提交邮寄信息";
        self.content = @"提交后邮寄地址无法修改，请确保邮寄地址正确之后再提交。";
        self.selectedTitles=@[@"取消",@"提交"];
    }

    
}
#pragma mark 设置样式
- (void)set_style {
    if (self.showType == Show_interrupt_test_type) { //中断考试
        NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:self.content];
        NSRange range = [self.content rangeOfString:@"30日"];
        [aStr addAttribute:NSForegroundColorAttributeName value:theme_color range:range];
        self.contentLabel.textAlignment = NSTextAlignmentLeft; 
        self.contentLabel.attributedText = aStr;
    } else if (self.showType == Show_replace_type) { //该账号已在其他设备登录
        self.cancel_btn.width = self.bottomView.width;
        [self.cancel_btn setTitleColor:theme_color forState:UIControlStateNormal];
        [self.bottomView bringSubviewToFront:self.cancel_btn];
    } else if (self.showType == Show_agreement_type) {
        self.contentLabel.textAlignment = NSTextAlignmentLeft;
    } else if (self.showType == Show_record_complete_type) { //到时间停止
         self.cancel_btn.width = self.bottomView.width;
       [self.cancel_btn setTitleColor:theme_color forState:UIControlStateNormal];
       [self.bottomView bringSubviewToFront:self.cancel_btn];
    } else if (self.showType == Show_mandatory_update_type) { //强制更新
        self.cancel_btn.width = self.bottomView.width;
          [self.cancel_btn setTitleColor:theme_color forState:UIControlStateNormal];
          [self.bottomView bringSubviewToFront:self.cancel_btn];
    }

}
- (void)btnClicked:(UIButton *)btn {
    if (self.showType == Show_mandatory_update_type) {//强制更新
        NSInteger tag = btn.tag-10;
        if (self.clickBolck) {
            self.clickBolck(tag);
        }
        return;
    }
    NSInteger tag = btn.tag-10;
    if (self.clickBolck) {
        self.clickBolck(tag);
    }
    [self removeFromSuperview];
}
+ (void)dismis {
    UIWindow *wind = [UIApplication sharedApplication].keyWindow;
    for (UIView *view in wind.subviews) {
        if ([view isKindOfClass:[self class]]) {
            [view removeFromSuperview];
            break;
        }
    }
}
+ (void)showType:(PopupWindowShowType)showType ClickBolck:(void(^)(NSInteger type))clickBolck {
    UIWindow *wind = [UIApplication sharedApplication].keyWindow;
    PopupWindow *selfView =[[PopupWindow alloc] initWithFrame:wind.bounds showType:showType];
    selfView.clickBolck = clickBolck;
    [wind addSubview:selfView];
}

+ (void)showType:(PopupWindowShowType)showType showContent:(NSString *)content ClickBolck:(void(^)(NSInteger type))clickBolck {
    UIWindow *wind = [UIApplication sharedApplication].keyWindow;
    PopupWindow *selfView =[[PopupWindow alloc] initWithFrame:wind.bounds showContent:content showType:showType];
    selfView.clickBolck = clickBolck;
    [wind addSubview:selfView];
}
//是否正在显示强制版本更新
+ (BOOL)is_show_Version_update {
      UIWindow *wind = [UIApplication sharedApplication].keyWindow;
      for (UIView *view in wind.subviews) {
          if ([view isKindOfClass:[PopupWindow class]]) {
              PopupWindow * selfView =(PopupWindow *)view;
              if (selfView.showType==Show_mandatory_update_type) {
                  return YES;
              }
          }
      }
    return NO;
}
@end
